#include <sysinfoapi.h>
#include "stdio.h"
#include "task.h"
#include "log.h"

Task_t *task1;
Task_t *task2;
Task_t *task3;
Task_t *task4;

uint64_t GetTick() {
    return (uint64_t) GetTickCount64();
}

void exampleTimer1Callback(Task_t *task, void *userData) {
    printf("[%012ld] Task:%p callback-> %s.\r\n", GetTick(), task, (char *) userData);
    TaskDel(task);
}

void exampleTimer2Callback(Task_t *task, void *userData) {
    printf("[%012ld] Task:%p callback-> %s.\r\n", GetTick(), task, (char *) userData);
}

void exampleTimer3Callback(Task_t *task, void *userData) {
    printf("[%012ld] Task:%p callback-> %s.\r\n", GetTick(), task, (char *) userData);
    TaskSetTime(task, 4567);
    TaskStart(task);
}

typedef struct CustomUserData {
    int count;
    char *str;
} CustomUserData;

void exampleTimer4Callback(Task_t *task, void *userData) {
    CustomUserData *customUserData = (CustomUserData *) userData;
    customUserData->count--;
    printf("[%012llu] Task:%p callback-> %s.\r\n", GetTick(), task, customUserData->str);
    if (customUserData->count > 0) {
        TaskStart(task);
    }
}

#undef LOG_RUN_TIMER_FUN
#define LOG_RUN_TIMER_FUN GetTick()
int Test_task(void *pVoid) {

    LOG_RUN_TIME("Init Timer", {
    }) {
        TaskInit(GetTick);
        TaskCreat(task1, 1000, -1, exampleTimer1Callback, "1000ms CYCLE task");
        TaskCreat(task2, 5000, -1, exampleTimer2Callback, "5000ms ONCE task");
        TaskCreat(task3, 3456, 2, exampleTimer3Callback, "3456ms delay start, 4567ms CYCLE task");
        CustomUserData customUserData = {
                .count = 3,
                .str = "2000ms 3 task"
        };
        TaskCreat(task4, 2000, 1, exampleTimer4Callback, &customUserData);
    }
    while (1) {
        TaskRun();
    }
}
